﻿using StoreSQLSugar_.DBUtility;
using StoreSQLSugar_.Helpers;
using StoreSQLSugar.Models;
using System;
using System.Reflection;
using System.Windows.Forms;

namespace StoreSQLSugar.Forms.StoreFrm
{
    public partial class StoreAddEditFrm : BaseAddEditFrm<Store>
    {
        public StoreAddEditFrm() : base()
        {
            InitializeComponent();
        }
        public StoreAddEditFrm(int editId) : base(editId)
        {
            InitializeComponent();
            FormText = "仓库";
        }

        public override void ClearAll()
        {
            txtStoreName.Text = "";
            txtStorePlace.Text = "";
            rtbRemark.Text = "";
        }

        public override void ShowOldValue()
        {
            txtStoreName.Text = editModel.StoreName;
            txtStorePlace.Text = editModel.StorePlace;
            rtbRemark.Text = editModel.Remark;
        }

        public override void btnSave_Click(object sender, EventArgs e)
        {
            // 1。校验
            if (string.IsNullOrWhiteSpace(txtStoreName.Text))
            {
                MessageBox.Show("仓库名称不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // 2。判断添加或修改
            if (EditId == 0)
            {
                // 3。操作数据库
                var model = new Store()
                {
                    StoreName = txtStoreName.Text,
                    StorePlace = txtStorePlace.Text,
                    Remark = rtbRemark.Text,
                    AreaCount = 0
                };

                int count = SqlSugarHelper.Db.Queryable<Store>().Where(s => s.StoreName == model.StoreName && s.Status == 0).Count();
                if (count > 0)
                {
                    MessageBox.Show("仓库名称重复！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                int row = SqlSugarHelper.Db.Insertable(model).ExecuteCommand();
                if (row <= 0)
                {
                    MessageBox.Show("添加数据有误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                // 3。操作数据库
                if (editModel == null)
                {
                    MessageBox.Show("修改数据有误，可能非法进入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                editModel.StoreName = txtStoreName.Text;
                editModel.StorePlace = txtStorePlace.Text;
                editModel.Remark = rtbRemark.Text;
                editModel.AreaCount = 0;
                editModel.LastUpdateUserId = LoginHelper.User.UserId;
                editModel.LastUpdateTime = DateTime.Now;

                int count = SqlSugarHelper.Db.Queryable<Store>().Where(s => s.StoreName == editModel.StoreName && s.StoreId != editModel.StoreId && s.Status==0).Count();
                if (count > 0)
                {
                    MessageBox.Show("仓库名称重复！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                int row = SqlSugarHelper.Db.Updateable(editModel).ExecuteCommand();
                if (row <= 0)
                {
                    MessageBox.Show("修改数据有误！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            // 4。返回列表窗体
            DialogResult = DialogResult.OK;
        }
    }
}
